package com.lemon.smartsafetynew.ui.fragment.record;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.ui.activity.record.RecordEquipmentDetailActivity;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshAddTitleFragment;
import com.lemon.smartsafetynew.entity.record.EquipVo;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.adapter.record.EquipInfoAdapter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

/**
 * Created by lzh on 2018/3/18.
 */

public class EquipmentFragment extends BasePullToRefreshAddTitleFragment<EquipVo, List<EquipVo>>
        implements RecycleChildItemClickListener<EquipVo> {
    private static EquipVo matMetalVo;
    private static MeCompanyVo companyVo;
    private String userId;
    public static EquipmentFragment newInstance(MeCompanyVo companyVo) {
        Bundle args = new Bundle();
        EquipmentFragment fragment = new EquipmentFragment();
        args.putSerializable(KeyConstants.KEY_COMPANY_VO, companyVo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        Bundle bundle = getArguments();
        if(bundle != null) {
            if(bundle.containsKey(KeyConstants.KEY_COMPANY_VO)) {
                companyVo = (MeCompanyVo) bundle.getSerializable(KeyConstants.KEY_COMPANY_VO);
            }
        }
        User userInfo = AccountUtil.getUser();
        userId = userInfo.getUserId();
    }

    @Override
    protected void initViews() {
        super.initViews();
        llTilte.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.company_space_count);
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("user_id", userId);
        params.put("pageNum",currentPage);
        params.put("pageSize",pageSize);
        RecordServices.init().getEquipInfo(RefreshType.REFRESH, params);
    }

    @Override
    public void onLoadMore() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("user_id", userId);
        params.put("pageNum",currentPage);
        params.put("pageSize",pageSize);
        RecordServices.init().getEquipInfo(RefreshType.LOAD_MORE, params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<EquipVo> getLemonAdapter() {
        return new EquipInfoAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_EQUIP_INFO_LIST;
    }

    @Override
    public void onDataList(List<EquipVo> data) {
        if(data != null && data.size() > 0) {
            tvTitleCount.setText("" + data.get(0).getTotalNumber());
        }else{
            tvTitleCount.setText("0");
        }
    }

    @Override
    public void onChildItemClick(EquipVo data, int position) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getPhone()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void onItemClick(EquipVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isShowReturn",companyVo.getIfShowReturn());
        bundle.putSerializable(KeyConstants.KEY_RECORD_EQUIPMENT_VO, data);
        UIHelper.showActivity(getmContext(), bundle, RecordEquipmentDetailActivity.class);
    }
}

